package servlet;

import com.alibaba.fastjson.JSONObject;
import enity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class AjaxServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        PrintWriter writer = resp.getWriter();
        System.out.println("进入AjaxServlet");
        //创建要输出的实体类
        User user = new User();
        user.setName("顾建宇");
        user.setSchoolId(2023022026);
        resp.setContentType("application/json");
        //将实体类转换成json对象，并转换成的json对象通过流传给前端
        Object json = JSONObject.toJSON(user);
        writer.println(json);
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }
}
